package com.xiaozhen.cmdb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozhen.cmdb.dao.CmdbModelAttributeDao;
import com.xiaozhen.cmdb.service.CmdbModelAttributeService;
import com.xiaozhen.common.core.domain.cmdb.CmdbModelAttribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/9 11:34
 * @description:
 */
@Service
public class CmdbModelAttributeImpl implements CmdbModelAttributeService {

    @Autowired
    private CmdbModelAttributeDao cmdbModelAttributeDao;

    @Override
    public Integer insertCmdbModelAttributeList(List<CmdbModelAttribute> list, Long currentUserId) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        Long modelId = list.get(0).getModelId();
        QueryWrapper<CmdbModelAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("model_id", modelId);
        cmdbModelAttributeDao.delete(queryWrapper);
        Integer count = 0;
        for (CmdbModelAttribute modelAttribute : list) {
            modelAttribute.setCreateBy(currentUserId);
            modelAttribute.setCreateTime(LocalDateTime.now());
            count += cmdbModelAttributeDao.insert(modelAttribute);
        }
        return count;
    }

    @Override
    public List<CmdbModelAttribute> selectCmdbModelAttributesByModelId(Long modelId, Long currentUserId) {
        QueryWrapper<CmdbModelAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("model_id", modelId);
        return cmdbModelAttributeDao.selectList(queryWrapper);
    }
}
